<?php
/**
 * Inbox Left Panel
 * 
 * This is the left panel component of the user inbox
 * 
 */

use Fox\Generator;
use Fox\Dao\SqlFragment;
use Hive\Mission;
use Orx\Mailer\MailBox\Message;
use Fox\Html\Wrapper;
use Fox\Html\Navigation\Menu\Item;
use Fox\Html\Navigation\Menu\Item\Sub;
use Fox\Html\Navigation\Menu;

$xview = Xview::instance();
$showAll = $xview->val('showall');

$wrapper = new Wrapper('inboxleftCol');
$master = 'messages';

// Counter 
$nbOfNewMessages = User::current()
					->countMessagesInStatus(array(
						
						Message::STATUS_UNREAD
					));
$nbOfDraftMessages = User::current()->countMessagesInStatus(Message::STATUS_DRAFT);

// Contact Summary
$menu = new Menu('standardMenu');

// Inbox
$menu->add(new Item('inbox',Msg(Orx\Mailer\Messages::BUNDLE,Orx\Mailer\Messages::INBOX),
						Generator::url($master,'list',null,
							array('status' => 'inbox','splitted'=>true)),
						$nbOfNewMessages
					)
				);

// Sent Messages
$menu->add(new Item('sent_messages',Msg(Orx\Mailer\Messages::BUNDLE,Orx\Mailer\Messages::SENT_MESSAGES),
						Generator::url($master,'list',null,
							array('status' => Message::STATUS_SENT)),
						null
					)
				);
// Drafts
$menu->add(new Item('drafts',Msg(Orx\Mailer\Messages::BUNDLE,Orx\Mailer\Messages::DRAFT_MESSAGES),
						Generator::url($master,'list',null,
							array('status' => Message::STATUS_DRAFT)),
						$nbOfDraftMessages
					)
				);
// Archive
$menu->add(new Item('archived',Msg(Orx\Mailer\Messages::BUNDLE,Orx\Mailer\Messages::ARCHIVED_MESSAGES),
						Generator::url($master,'list',null,
							array('status' => 'all')),
						null
					)
				);
// Deleted
$menu->add(new Item('deleted',Msg(Orx\Mailer\Messages::BUNDLE,Orx\Mailer\Messages::DELETE_BOX),
						Generator::url($master,'list',null,
							array('status' => Message::STATUS_DELETED)),
						null
					)
				);
$wrapper->add($menu);
// Dynamic Directories
$menuDynamicDirectory = new Menu('dynamicMenu',Msg(Hive::BUNDLE,Hive::TITLE_MISSIONS));
// Load all open missions
$query = \Fox\Dao::infos(Mission::className())->handle()->createQueryBuilder()
	->select('missionid','reference','title')
	->from('missions','m')
	->orderBy('reference');

//@TODO Handle Grouping
//$fragments[] = new SqlFragment('( grouping IN (?) OR grouping IS NULL')

if($showAll) {
	$fragments[] = new SqlFragment('loekz = ?',array(0));
} else {
	$fragments[] = new SqlFragment('status = ? AND loekz = ?',array(Mission::STATUS_OPEN,0));
}

$fragment = SqlFragment::combine($fragments);
$openMissions = $fragment->pushIn($query)->execute()->fetchAll(PDO::FETCH_ASSOC);
foreach($openMissions as $missionData) {
	$title = empty($missionData['reference']) ? $missionData['title'] : $missionData['reference'];
	$menuDynamicDirectory->add(new Item('job_'.$missionData['missionid'],
						$title,
						Generator::url($master,'list',null,
							array('area' => 'mission',
								  'reference' => $missionData['missionid'],
									'reference_class_id' => Mission::MESSAGE_REFERENCE)),
						null
					)
	);
}
// Show All ?
$wrapper->add($menuDynamicDirectory);
return $wrapper;